﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.DialogUI {
    import flash.events.*;
    import Events.*;
    import flash.utils.*;
    import org.aswing.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Display.*;
    import Display.MarketUI.*;
    import Modules.stats.experiments.*;
    import Display.aswingui.*;

    public class InstantReadyDialogView extends GenericDialogView {

        private static var m_confirmationFlag:Boolean = false;

        private var m_harvestable:HarvestableResource;

        public function InstantReadyDialogView(_arg1:Dictionary, _arg2:HarvestableResource){
            this.m_harvestable = _arg2;
            super(_arg1, ZLoc.t("Dialogs", ("InstantHarvestTitle_" + this.m_harvestable.getTypeName())), ("instant_harvest_" + this.m_harvestable.getTypeName()), TYPE_OK, null, "none", GenericDialogView.ICON_POS_BOTTOM);
        }
        override protected function createTextComponent(_arg1:Number):Component{
            var _local2:Component;
            var _local5:Number;
            var _local6:Container;
            var _local3:Component = ASwingHelper.makeMultilineText(ZLoc.t("Dialogs", ("InstantHarvestTitle_" + this.m_harvestable.getTypeName())), _arg1, EmbeddedArt.DEFAULT_FONT_NAME_BOLD, m_align, 18, EmbeddedArt.brownTextColor);
            var _local4:Number = 40;
            if (_local3.getHeight() < _local4){
                _local5 = (_local4 - _local3.getHeight());
                _local6 = ASwingHelper.makeSoftBoxJPanelVertical();
                _local6.append(_local3);
                _local6.append(ASwingHelper.verticalStrut(_local5));
                _local2 = _local6;
            } else {
                _local2 = _local3;
            };
            return (_local2);
        }
        override protected function createIconPane():JPanel{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local3:MarketCell = new MarketCell();
            _local3.setAssetDict(m_assetDict);
            _local3.setCellValue(this.getItemFromObject());
            _local3.setBuyable(false);
            _local2.append(_local3);
            var _local4:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.Y_AXIS, 5);
            var _local5:CustomButton = new CustomButton(String(this.getSingleItemCost()), new AssetIcon(new (m_assetDict["icon_cash"])()), "CashButtonUI");
            _local5.addEventListener(MouseEvent.MOUSE_UP, this.onReadyOneType, false, 0, true);
            _local5.setPreferredWidth(80);
            var _local6:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            _local6.appendAll(_local5);
            var _local7:CustomButton = new CustomButton(String(this.getInstantReadyCashCost()), new AssetIcon(new (m_assetDict["icon_cash"])()), "CashButtonUI");
            _local7.addEventListener(MouseEvent.MOUSE_UP, this.onReadyAllItemsOfType, false, 0, true);
            _local7.setPreferredWidth(80);
            var _local8:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            _local8.appendAll(_local7);
            var _local9:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.Y_AXIS, 5);
            var _local10:Object = {};
            _local10[this.m_harvestable.getTypeName()] = ZLoc.t("Items", (this.m_harvestable.getHarvestingDefinition().name + "_friendlyName"));
            _local9.appendAll(ASwingHelper.makeLabel(ZLoc.t("Dialogs", ("InstantHarvestThis_" + this.m_harvestable.getTypeName()), _local10), EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 16, EmbeddedArt.darkBlueTextColor), _local6);
            var _local11:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.Y_AXIS, 5);
            _local11.appendAll(ASwingHelper.makeLabel(ZLoc.t("Dialogs", ("InstantHarvestAll_" + this.m_harvestable.getTypeName())), EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 16, EmbeddedArt.darkBlueTextColor), _local8);
            _local4.appendAll(_local9, _local11);
            _local1.appendAll(_local2, ASwingHelper.horizontalStrut(15), _local4);
            return (_local1);
        }
        override protected function createButtonPanel():JPanel{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            _local1.appendAll(ASwingHelper.verticalStrut(45));
            return (_local1);
        }
        protected function onCancelClick(_arg1:MouseEvent):void{
            countDialogViewAction(ZLoc.t("Dialogs", "Cancel"));
            closeMe();
        }
        private function getItemFromObject():Item{
            return (this.m_harvestable.getHarvestingDefinition());
        }
        private function onReadyOneType(_arg1:Event):void{
            var _local3:Object;
            if ((((_arg1 is GenericPopupEvent)) && (!(((_arg1 as GenericPopupEvent).button == GenericDialogView.YES))))){
                InstantReadyDialogView.m_confirmationFlag = false;
                closeMe();
                return;
            };
            var _local2:int = this.getSingleItemCost();
            if (InstantReadyDialogView.m_confirmationFlag == false){
                InstantReadyDialogView.m_confirmationFlag = true;
                _local3 = {};
                _local3[this.m_harvestable.getTypeName()] = ZLoc.t("Items", (this.m_harvestable.getHarvestingDefinition().name + "_friendlyName"));
                _local3["amount"] = _local2;
                UI.displayMessage(ZLoc.t("Dialogs", (("InstantHarvestThis_" + this.m_harvestable.getTypeName()) + "_buy"), _local3), GenericDialogView.TYPE_YESNO, this.onReadyOneType, "", true);
                closeMe();
            } else {
                if (Global.player.cash < _local2){
                    UI.displayPopup(new GetCoinsDialog(ZLoc.t("Dialogs", "ImpulseMarketCash"), "", GenericDialogView.TYPE_YESNO, null, true));
                    closeMe();
                } else {
                    Global.player.cash = (Global.player.cash - _local2);
                    GameTransactionManager.addTransaction(new TInstantReady(this.m_harvestable), true);
                    closeMe();
                };
            };
        }
        private function onReadyAllItemsOfType(_arg1:Event):void{
            var _local3:Object;
            if ((((_arg1 is GenericPopupEvent)) && (!(((_arg1 as GenericPopupEvent).button == GenericDialogView.YES))))){
                InstantReadyDialogView.m_confirmationFlag = false;
                closeMe();
                return;
            };
            var _local2:int = this.getInstantReadyCashCost();
            if (InstantReadyDialogView.m_confirmationFlag == false){
                InstantReadyDialogView.m_confirmationFlag = true;
                _local3 = {};
                _local3["amount"] = _local2;
                UI.displayMessage(ZLoc.t("Dialogs", (("InstantHarvestAll_" + this.m_harvestable.getTypeName()) + "_buy"), _local3), GenericDialogView.TYPE_YESNO, this.onReadyAllItemsOfType, "", true);
                closeMe();
            } else {
                if (Global.player.cash < _local2){
                    UI.displayPopup(new GetCoinsDialog(ZLoc.t("Dialogs", "ImpulseMarketCash"), "", GenericDialogView.TYPE_YESNO, null, true));
                    closeMe();
                } else {
                    Global.player.cash = (Global.player.cash - _local2);
                    GameTransactionManager.addTransaction(new TInstantReady(null, this.m_harvestable.getTypeName()), true);
                    closeMe();
                };
            };
        }
        private function getInstantReadyCashCost():int{
            var _local1:int;
            var _local11:HarvestableResource;
            var _local2:Number = 1;
            var _local3:Number = 1;
            var _local4:String = this.m_harvestable.getTypeName();
            switch (_local4){
                case "plot":
                case "ship":
                    _local1 = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_INSTANT_READY_CROPS_SHIPS);
                    _local2 = Number(RuntimeVariableManager.getString("INSTANT_READY_CROP_EXPONENT", "0.4"));
                    _local3 = Global.gameSettings.getInstantReadyCropMultiplier(_local1);
                    break;
                case "residence":
                    _local1 = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_INSTANT_READY_RESIDENCES);
                    _local2 = Number(RuntimeVariableManager.getString("INSTANT_READY_RESIDENCE_EXPONENT", "0.25"));
                    _local3 = Global.gameSettings.getInstantReadyResidenceMultiplier(_local1);
                    break;
            };
            var _local5:int = this.getWorldObjectType(_local4);
            var _local6:Array = Global.world.getObjectsByTypes([_local5]);
            var _local7:Number = 0;
            var _local8:Number = 0;
            var _local9:int;
            while (_local9 < _local6.length) {
                _local11 = _local6[_local9];
                if (_local11.getState() == HarvestableResource.STATE_PLANTED){
                    _local7 = (Number(_local11.getGrowTimeLeft()) / 3600);
                    _local8 = (_local8 + (_local3 * Math.pow(_local7, _local2)));
                };
                _local9++;
            };
            var _local10:Number = Math.max(Math.ceil(_local8), 1);
            return (_local10);
        }
        private function getSingleItemCost():int{
            var _local2:int;
            var _local1:Number = (Number(this.m_harvestable.getGrowTimeLeft()) / 3600);
            var _local3:Number = 1;
            var _local4:Number = 1;
            var _local5:String = this.m_harvestable.getTypeName();
            switch (_local5){
                case "plot":
                case "ship":
                    _local2 = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_INSTANT_READY_CROPS_SHIPS);
                    _local3 = Number(RuntimeVariableManager.getString("INSTANT_READY_CROP_EXPONENT", "0.4"));
                    _local4 = Global.gameSettings.getInstantReadyCropMultiplier(_local2);
                    break;
                case "residence":
                    _local2 = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_INSTANT_READY_RESIDENCES);
                    _local3 = Number(RuntimeVariableManager.getString("INSTANT_READY_RESIDENCE_EXPONENT", "0.25"));
                    _local4 = Global.gameSettings.getInstantReadyResidenceMultiplier(_local2);
                    break;
            };
            var _local6:Number = (_local4 * Math.pow(_local1, _local3));
            _local6 = Math.max(Math.ceil(_local6), 1);
            return (_local6);
        }
        private function getAllItemCost():int{
            var _local1:int;
            var _local2:int = this.getNumPlantedItems(this.m_harvestable.getTypeName());
            var _local3:Number = this.getHoursLeftForType(this.m_harvestable.getTypeName());
            switch (this.m_harvestable.getTypeName()){
                case "plot":
                case "ship":
                    _local1 = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_INSTANT_READY_CROPS_SHIPS);
                    return (this.getCropCost(_local3, _local2, _local1));
                case "residence":
                    _local1 = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_INSTANT_READY_RESIDENCES);
                    return (this.getResidenceCost(_local3, _local2, _local1));
            };
            return (Number.POSITIVE_INFINITY);
        }
        private function getHoursLeftForType(_arg1:String):Number{
            var _local6:HarvestableResource;
            var _local2:int = this.getWorldObjectType(_arg1);
            var _local3:Array = Global.world.getObjectsByTypes([_local2]);
            var _local4:Number = 0;
            var _local5:int;
            while (_local5 < _local3.length) {
                _local6 = _local3[_local5];
                if (_local6.getState() == HarvestableResource.STATE_PLANTED){
                    _local4 = (_local4 + (Number(_local6.getGrowTimeLeft()) / 3600));
                };
                _local5++;
            };
            return (_local4);
        }
        private function getNumPlantedItems(_arg1:String):Number{
            var _local6:HarvestableResource;
            var _local2:int = this.getWorldObjectType(_arg1);
            var _local3:Array = Global.world.getObjectsByTypes([_local2]);
            var _local4:Number = 0;
            var _local5:int;
            while (_local5 < _local3.length) {
                _local6 = _local3[_local5];
                if (_local6.getState() == HarvestableResource.STATE_PLANTED){
                    _local4++;
                };
                _local5++;
            };
            return (_local4);
        }
        private function getWorldObjectType(_arg1:String):int{
            switch (_arg1){
                case "plot":
                    return (WorldObjectTypes.PLOT);
                case "ship":
                    return (WorldObjectTypes.SHIP);
                case "residence":
                    return (WorldObjectTypes.RESIDENCE);
            };
            return (Number.NaN);
        }
        private function getCropCost(_arg1:Number, _arg2:int, _arg3:int):int{
            if (_arg2 <= 0){
                return (1);
            };
            var _local4:Number = Number(RuntimeVariableManager.getString("INSTANT_READY_CROP_EXPONENT", "0.4"));
            var _local5:Number = Global.gameSettings.getInstantReadyCropMultiplier(_arg3);
            var _local6:Number = Math.max(Math.ceil(((_arg2 * _local5) * Math.pow((_arg1 / _arg2), _local4))), 1);
            return (_local6);
        }
        private function getResidenceCost(_arg1:Number, _arg2:int, _arg3:int):int{
            if (_arg2 <= 0){
                return (1);
            };
            var _local4:Number = Number(RuntimeVariableManager.getString("INSTANT_READY_RESIDENCE_EXPONENT", "0.25"));
            var _local5:Number = Global.gameSettings.getInstantReadyResidenceMultiplier(_arg3);
            var _local6:Number = Math.max(Math.ceil(((_arg2 * _local5) * Math.pow((_arg1 / _arg2), _local4))), 1);
            return (_local6);
        }

    }
}//package Display.DialogUI 
